package servicesImp.dictionary;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.dictionary.MajorTypeServices;
import wt.dao.dictionary.MajorTypeMapper;
import wt.entity.dictionary.MajorType;
import wt.entity.dictionary.MajorTypeExample;
import wt.staticData.ShortData;
import wt.staticData.TableName;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/14.
 */


@Service
public class MajorTypeServicesImp extends IBaseServicesWithoutBLOBsImpl<MajorType, MajorTypeExample> implements MajorTypeServices {
    @Autowired
    MajorTypeMapper majorTypeMapper;

    @Override
    public IBaseMapperWithoutBLOBs<MajorType, MajorTypeExample> getMapper() {
        return this.majorTypeMapper;
    }

    @Override
    public String getTableName() {
        return TableName.DIC_MAJOR_TYPE;
    }


    public List<MajorType> selectMajorType(String itemId, boolean isOnlyLive) throws Exception {

        MajorTypeExample majorTypeExample = new MajorTypeExample();
        majorTypeExample.setOrderByClause("num");
        MajorTypeExample.Criteria criteria = majorTypeExample.createCriteria();
        if (isOnlyLive == true) {
            criteria.andLiveEqualTo(ShortData.s1);
        }

        if (!StringUtils.isBlank(itemId)) {
            criteria.andItemIdEqualTo(itemId);
        }

        return selectByExample(majorTypeExample);
    }


    @Transactional
    public void updateMajorTypeLive(List<String> setLiveIdList, List<String> setNoLiveIdList) throws Exception {
        MajorType majorType = new MajorType();
        majorType.setLive((short) 1);
        for (int i = 0; setLiveIdList != null && i < setLiveIdList.size(); i = i + 1000) {
            MajorTypeExample majorTypeExample = new MajorTypeExample();
            majorTypeExample.createCriteria().andIdIn(setLiveIdList.subList(i, setLiveIdList.size() - i >= 1000 ? i + 1000 : setLiveIdList.size()));
            getCurrentProxy().updateByExampleSelective(majorType, majorTypeExample);
        }


        majorType.setLive((short) 0);
        for (int i = 0; setNoLiveIdList != null && i < setNoLiveIdList.size(); i = i + 1000) {
            MajorTypeExample majorTypeExample = new MajorTypeExample();
            majorTypeExample.createCriteria().andIdIn(setNoLiveIdList.subList(i, setNoLiveIdList.size() - i >= 1000 ? i + 1000 : setNoLiveIdList.size()));
            getCurrentProxy().updateByExampleSelective(majorType, majorTypeExample);
        }
    }

    public List<MajorType> selectTreeByLeafId(List<String> leafIdList) {
        if (leafIdList == null || leafIdList.size() == 0) {
            return new ArrayList<MajorType>();
        }
        return majorTypeMapper.selectTreeByLeafId(leafIdList);
    }

    public List<MajorType> selectTreeByLeafNum(List<String> leafNumList) {
        if (leafNumList == null || leafNumList.size() == 0) {
            return new ArrayList<MajorType>();
        }
        return majorTypeMapper.selectTreeByLeafNum(leafNumList);
    }

    public List<MajorType> selectExpertMajorByLevel(String expertId, String itemId, Integer level) {
        if (StringUtils.isBlank(expertId)){
            return  new ArrayList<MajorType>();
        }
        return majorTypeMapper.selectExpertMajorByLevel(expertId, itemId, level);
    }

    public List<MajorType> selectByMajorNumList(List<String> majorNumList) throws Exception {
        if (majorNumList == null || majorNumList.isEmpty()) {
            return new ArrayList<MajorType>();
        }
        MajorTypeExample majorTypeExample = new MajorTypeExample();
        for (int i = 0; i < majorNumList.size(); i += 1000) {
            majorTypeExample.or().andNumIn(majorNumList.subList(i, majorNumList.size() <= i + 1000 ? majorNumList.size() : i + 1000));
        }
        List<MajorType> majorTypeList = majorTypeMapper.selectByExample(majorTypeExample);
        return majorTypeList;
    }

    /**
     * 查询业务专业等级下的专业
     *
     * @param businessId 不能为空
     * @param level      可为空
     * @return
     */
    public List<MajorType> selectBusinessMajorByLevel(String businessId, Integer level) {
        return majorTypeMapper.selectBusinessMajorByLevel(businessId,level);
    }
}
